package com.takepoints21.mobile;

import com.takepoints.android.bo.RewardDetail;
import com.takepoints.android.services.TPServices;
import com.takepoints21.mobile.zxing.CaptureActivity;
import com.takepoints.android.util.DownloadImageTask;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TableRow;
import android.widget.TextView;

public class MyCardRewardDetail extends BaseActivity {
	
	private final String IMG_REWARD_PATH = "https://www.takepoints.com/public/uploads/reward/";
	
	private String cardId;
	private String promotionId;
	private String rewardNum;
	
	public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mycardrewarddetail);
        manageMenu(0);
        
        Bundle bundle = this.getIntent().getExtras();
        cardId = bundle.getString("cardId");
        promotionId = bundle.getString("promotionId");
        rewardNum = bundle.getString("rewardNum");
        //alert("cardId = " + cardId + "\npromotionId = " + promotionId + "\nrewardNum = " + rewardNum);
        
        RewardDetail detail = TPServices.getRewardDetail(promotionId, rewardNum);
        
        ImageView imgReward = (ImageView)findViewById(R.id.imgReward);
        //imgReward.setImageDrawable(ImageUtil.getImageURL(IMG_REWARD_PATH + detail.getPicName()));
        if(imgReward != null){
			Bitmap bitmap = BitmapFactory.decodeFile(IMG_CACHE_PATH + detail.getPicName());
			if(bitmap != null){
				imgReward.setImageBitmap(bitmap);
			} else{
				imgReward.setTag(IMG_REWARD_PATH + detail.getPicName());
				new DownloadImageTask().execute(imgReward);
			}
		}
        
        TableRow trRewardDetail = (TableRow)findViewById(R.id.trRewardDetail);
        
        TextView txtRewardTitle = new TextView(trRewardDetail.getContext());
        txtRewardTitle.setText(detail.getCardRewardTitle());
        txtRewardTitle.setTextSize(11);
        txtRewardTitle.setTextColor(Color.parseColor("#404a4a"));
        txtRewardTitle.setSingleLine(false);
        trRewardDetail.addView(txtRewardTitle);
        
        Button btnReward = (Button)findViewById(R.id.btnReward);
        btnReward.setOnClickListener(new View.OnClickListener(){
            public void onClick(View view){
            	gotoVerifyCode();
            }
    	});
	}
    
    private final void gotoVerifyCode(){
    	//Intent verifyInt = new Intent(this, VerifyCode.class);
    	Intent verifyInt = new Intent(this, CaptureActivity.class);
		verifyInt.putExtra("pageSrc", "MyCardRewardDetail");
    	verifyInt.putExtra("cardId", cardId);
		verifyInt.putExtra("promotionId", promotionId);
		verifyInt.putExtra("rewardNum", rewardNum);
    	startActivityForResult(verifyInt, 0);
    }
	
	@Override
	public void onBackPressed(){
		Intent cardDetailInt = new Intent(this, MyCardDetail.class);
		cardDetailInt.putExtra("cardId", String.valueOf(cardId));
		startActivityForResult(cardDetailInt, 0);
		return;
	}
	
}
